import logging
from collections import OrderedDict
from typing import Any, List

import gymnasium as gym
import numpy as np
import tree

ATARI_OBS_SHAPE = (210, 160, 3)
ATARI_RAM_OBS_SHAPE = (128,)

# Only validate env observations vs the observation space every n times in a
# Preprocessor.
OBS_VALIDATION_INTERVAL = 100

logger = logging.getLogger(__name__)


def convert_element_to_space_type(element: Any, sampled_element: Any) -> Any:
    """Convert all the components of the element to match the space dtypes.

    Args:
        element: The element to be converted.
        sampled_element: An element sampled from a space to be matched
            to.

    Returns:
        The input element, but with all its components converted to match
        the space dtypes.
    """

    def map_(elem, s):
        if isinstance(s, np.ndarray):
            if not isinstance(elem, np.ndarray):
                assert isinstance(
                    elem, (float, int)
                ), f"ERROR: `elem` ({elem}) must be np.array, float or int!"
                if s.shape == ():
                    elem = np.array(elem, dtype=s.dtype)
                else:
                    raise ValueError(
                        "Element should be of type np.ndarray but is instead of \
                            type {}".format(
                            type(elem)
                        )
                    )
            elif s.dtype != elem.dtype:
                elem = elem.astype(s.dtype)

        # Gymnasium now uses np.int_64 as the dtype of a Discrete action space
        elif isinstance(s, int) or isinstance(s, np.int_):
            if isinstance(elem, float) and elem.is_integer():
                elem = int(elem)
            # Note: This does not check if the float element is actually an integer
            if isinstance(elem, np.float_):
                elem = np.int64(elem)

        return elem

    return tree.map_structure(map_, element, sampled_element, check_types=False)


class Preprocessor:
    """Defines an abstract observation preprocessor function.

    Attributes:
        shape (List[int]): Shape of the preprocessed output.
    """

    def __init__(self, obs_space: gym.Space, options: dict = None):
        _legacy_patch_shapes(obs_space)
        self._obs_space = obs_space
        if not options:
            from ray.rllib.models.catalog import MODEL_DEFAULTS

            self._options = MODEL_DEFAULTS.copy()
        else:
            self._options = options
        self.shape = self._init_shape(obs_space, self._options)
        self._size = int(np.product(self.shape))
        self._i = 0
        self._obs_for_type_matching = self._obs_space.sample()

    def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]:
        """Returns the shape after preprocessing."""
        raise NotImplementedError

    def transform(self, observation) -> np.ndarray:
        """Returns the preprocessed observation."""
        raise NotImplementedError

    def write(self, observation, array: np.ndarray, offset: int) -> None:
        """Alternative to transform for more efficient flattening."""
        array[offset : offset + self._size] = self.transform(observation)

    def check_shape(self, observation: Any) -> None:
        """Checks the shape of the given observation."""
        if self._i % OBS_VALIDATION_INTERVAL == 0:
            # Convert lists to np.ndarrays.
            if type(observation) is list and isinstance(
                self._obs_space, gym.spaces.Box
            ):
                observation = np.array(observation).astype(np.float32)
            if not self._obs_space.contains(observation):
                observation = convert_element_to_space_type(
                    observation, self._obs_for_type_matching
                )
            try:
                if not self._obs_space.contains(observation):
                    raise ValueError(
                        "Observation ({} dtype={}) outside given space ({})!".format(
                            observation,
                            (
                                observation.dtype
                                if isinstance(self._obs_space, gym.spaces.Box)
                                else None
                            ),
                            self._obs_space,
                        )
                    )
            except AttributeError as e:
                raise ValueError(
                    "Observation for a Box/MultiBinary/MultiDiscrete space "
                    "should be an np.array, not a Python list.",
                    observation,
                ) from e
        self._i += 1

    @property
    def size(self) -> int:
        return self._size

    @property
    def observation_space(self) -> gym.Space:
        obs_space = gym.spaces.Box(-1.0, 1.0, self.shape, dtype=np.float32)
        # Stash the unwrapped space so that we can unwrap dict and tuple spaces
        # automatically in modelv2.py
        classes = (
            DictFlatteningPreprocessor,
            OneHotPreprocessor,
            RepeatedValuesPreprocessor,
            TupleFlatteningPreprocessor,
            AtariRamPreprocessor,
        )
        if isinstance(self, classes):
            obs_space.original_space = self._obs_space
        return obs_space


class AtariRamPreprocessor(Preprocessor):

    def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]:
        return (128,)

    def transform(self, observation) -> np.ndarray:
        self.check_shape(observation)
        return (observation.astype("float32") - 128) / 128


class OneHotPreprocessor(Preprocessor):
    """One-hot preprocessor for Discrete and MultiDiscrete spaces.

    Examples:
        >>> self.transform(Discrete(3).sample())
        ... np.array([0.0, 1.0, 0.0])
        >>> self.transform(MultiDiscrete([2, 3]).sample())
        ... np.array([0.0, 1.0, 0.0, 0.0, 1.0])
    """

    def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]:
        if isinstance(obs_space, gym.spaces.Discrete):
            return (self._obs_space.n,)
        else:
            return (np.sum(self._obs_space.nvec),)

    def transform(self, observation) -> np.ndarray:
        self.check_shape(observation)
        return gym.spaces.utils.flatten(self._obs_space, observation).astype(np.float32)

    def write(self, observation, array: np.ndarray, offset: int) -> None:
        array[offset : offset + self.size] = self.transform(observation)


class NoPreprocessor(Preprocessor):

    def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]:
        return self._obs_space.shape

    def transform(self, observation) -> np.ndarray:
        self.check_shape(observation)
        return observation

    def write(self, observation, array: np.ndarray, offset: int) -> None:
        array[offset : offset + self._size] = np.array(observation, copy=False).ravel()

    @property
    def observation_space(self) -> gym.Space:
        return self._obs_space


class MultiBinaryPreprocessor(Preprocessor):
    """Preprocessor that turns a MultiBinary space into a Box.

    Note: Before RLModules were introduced, RLlib's ModelCatalogV2 would produce
    ComplexInputNetworks that treat MultiBinary spaces as Boxes. This preprocessor is
    needed to get rid of the ComplexInputNetworks and use RLModules instead because
    RLModules lack the logic to handle MultiBinary or other non-Box spaces.
    """

    def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]:
        return self._obs_space.shape

    def transform(self, observation) -> np.ndarray:
        # The shape stays the same, but the dtype changes.
        self.check_shape(observation)
        return observation.astype(np.float32)

    def write(self, observation, array: np.ndarray, offset: int) -> None:
        array[offset : offset + self._size] = np.array(observation, copy=False).ravel()

    @property
    def observation_space(self) -> gym.Space:
        obs_space = gym.spaces.Box(0.0, 1.0, self.shape, dtype=np.float32)
        obs_space.original_space = self._obs_space
        return obs_space


class TupleFlatteningPreprocessor(Preprocessor):
    """Preprocesses each tuple element, then flattens it all into a vector.

    RLlib models will unpack the flattened output before _build_layers_v2().
    """

    def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]:
        assert isinstance(self._obs_space, gym.spaces.Tuple)
        size = 0
        self.preprocessors = []
        for i in range(len(self._obs_space.spaces)):
            space = self._obs_space.spaces[i]
            logger.debug("Creating sub-preprocessor for {}".format(space))
            preprocessor_class = get_preprocessor(space)
            if preprocessor_class is not None:
                preprocessor = preprocessor_class(space, self._options)
                size += preprocessor.size
            else:
                preprocessor = None
                size += int(np.product(space.shape))
            self.preprocessors.append(preprocessor)
        return (size,)

    def transform(self, observation) -> np.ndarray:
        self.check_shape(observation)
        array = np.zeros(self.shape, dtype=np.float32)
        self.write(observation, array, 0)
        return array

    def write(self, observation, array: np.ndarray, offset: int) -> None:
        assert len(observation) == len(self.preprocessors), observation
        for o, p in zip(observation, self.preprocessors):
            p.write(o, array, offset)
            offset += p.size


class DictFlatteningPreprocessor(Preprocessor):
    """Preprocesses each dict value, then flattens it all into a vector.

    RLlib models will unpack the flattened output before _build_layers_v2().
    """

    def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]:
        assert isinstance(self._obs_space, gym.spaces.Dict)
        size = 0
        self.preprocessors = []
        for space in self._obs_space.spaces.values():
            logger.debug("Creating sub-preprocessor for {}".format(space))
            preprocessor_class = get_preprocessor(space)
            if preprocessor_class is not None:
                preprocessor = preprocessor_class(space, self._options)
                size += preprocessor.size
            else:
                preprocessor = None
                size += int(np.product(space.shape))
            self.preprocessors.append(preprocessor)
        return (size,)

    def transform(self, observation) -> np.ndarray:
        self.check_shape(observation)
        array = np.zeros(self.shape, dtype=np.float32)
        self.write(observation, array, 0)
        return array

    def write(self, observation, array: np.ndarray, offset: int) -> None:
        if not isinstance(observation, OrderedDict):
            observation = OrderedDict(sorted(observation.items()))
        assert len(observation) == len(self.preprocessors), (
            len(observation),
            len(self.preprocessors),
        )
        for o, p in zip(observation.values(), self.preprocessors):
            p.write(o, array, offset)
            offset += p.size


class RepeatedValuesPreprocessor(Preprocessor):
    """Pads and batches the variable-length list value."""

    def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]:
        assert isinstance(self._obs_space, Repeated)
        child_space = obs_space.child_space
        self.child_preprocessor = get_preprocessor(child_space)(
            child_space, self._options
        )
        # The first slot encodes the list length.
        size = 1 + self.child_preprocessor.size * obs_space.max_len
        return (size,)

    def transform(self, observation) -> np.ndarray:
        array = np.zeros(self.shape)
        if isinstance(observation, list):
            for elem in observation:
                self.child_preprocessor.check_shape(elem)
        else:
            pass  # ValueError will be raised in write() below.
        self.write(observation, array, 0)
        return array

    def write(self, observation, array: np.ndarray, offset: int) -> None:
        if not isinstance(observation, (list, np.ndarray)):
            raise ValueError(
                "Input for {} must be list type, got {}".format(self, observation)
            )
        elif len(observation) > self._obs_space.max_len:
            raise ValueError(
                "Input {} exceeds max len of space {}".format(
                    observation, self._obs_space.max_len
                )
            )
        # The first slot encodes the list length.
        array[offset] = len(observation)
        for i, elem in enumerate(observation):
            offset_i = offset + 1 + i * self.child_preprocessor.size
            self.child_preprocessor.write(elem, array, offset_i)


def get_preprocessor(space: gym.Space, include_multi_binary=False) -> type:
    """Returns an appropriate preprocessor class for the given space."""

    _legacy_patch_shapes(space)
    obs_shape = space.shape

    if isinstance(space, (gym.spaces.Discrete, gym.spaces.MultiDiscrete)):
        preprocessor = OneHotPreprocessor
    elif obs_shape == ATARI_OBS_SHAPE:
        logger.debug(
            "Defaulting to RLlib's GenericPixelPreprocessor because input "
            "space has the atari-typical shape {}. Turn this behaviour off by setting "
            "`preprocessor_pref=None` or "
            "`preprocessor_pref='deepmind'` or disabling the preprocessing API "
            "altogether with `_disable_preprocessor_api=True`.".format(ATARI_OBS_SHAPE)
        )
        preprocessor = GenericPixelPreprocessor
    elif obs_shape == ATARI_RAM_OBS_SHAPE:
        logger.debug(
            "Defaulting to RLlib's AtariRamPreprocessor because input "
            "space has the atari-typical shape {}. Turn this behaviour off by setting "
            "`preprocessor_pref=None` or "
            "`preprocessor_pref='deepmind' or disabling the preprocessing API "
            "altogether with `_disable_preprocessor_api=True`."
            "`.".format(ATARI_OBS_SHAPE)
        )
        preprocessor = AtariRamPreprocessor
    elif isinstance(space, gym.spaces.Tuple):
        preprocessor = TupleFlatteningPreprocessor
    elif isinstance(space, gym.spaces.Dict):
        preprocessor = DictFlatteningPreprocessor
    elif isinstance(space, Repeated):
        preprocessor = RepeatedValuesPreprocessor
    # We usually only want to include this when using RLModules
    elif isinstance(space, gym.spaces.MultiBinary) and include_multi_binary:
        preprocessor = MultiBinaryPreprocessor
    else:
        preprocessor = NoPreprocessor

    return preprocessor


def _legacy_patch_shapes(space: gym.Space) -> List[int]:
    """Assigns shapes to spaces that don't have shapes.

    This is only needed for older gym versions that don't set shapes properly
    for Tuple and Discrete spaces.
    """

    if not hasattr(space, "shape"):
        if isinstance(space, gym.spaces.Discrete):
            space.shape = ()
        elif isinstance(space, gym.spaces.Tuple):
            shapes = []
            for s in space.spaces:
                shape = _legacy_patch_shapes(s)
                shapes.append(shape)
            space.shape = tuple(shapes)

    return space.shape
